package com.zx.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.springframework.util.StopWatch;

import com.zx.ParseFactory;
import com.zx.exception.DataParseException;
import com.zx.process.DataConfigProcess;
import com.zx.reader.impl.TextFileReader;
/**
 * @Project: dataParse-test
 * @Title: Demo
 * @Description: 性能测试
 * @author: xue.zhang
 * @date: 2018年5月21日下午2:34:36
 * @company: yooli
 * @Copyright: Copyright (c) 2017
 * @version v1.0
 */
public class Demo {

	private static final String ROOT_PATH = "D://demo/";
//	测试1
//	private static final int step = 1000 * 10;
//	private static final int num = 100;
//	测试2
//	private static final int step = 1000 * 20;
//	private static final int num = 100;
//	测试3	
//	private static final int step = 1000 * 100;
//	private static final int num = 100;
//	测试5	
	private static final int step = 1000 * 1000 * 5;
	private static final int num = 10;
	
	//@Test
	public void create() {
		StopWatch sw = new StopWatch();
		for (int i = 0; i < num; i++) {
			String filePath =  ROOT_PATH + (i + 1) * step + ".txt";
			sw.start("创建：" + filePath);
			try {
				BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filePath)));
				for (int j = 0; j < step * (i + 1); j++) {
					bw.write("1 张三 1900-01-01\r\n");
				}
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			sw.stop();
		}
		System.out.println(sw.prettyPrint());
	}
	
	//@Test
	public void read() throws Exception {
		// 注册类的解析器，自动返回一个解析器
		DataConfigProcess<User> dataConfigProcess = ParseFactory.register(User.class);
		
		StopWatch sw = new StopWatch();
		for (int i = 0; i < num; i++) {
			String filePath =  ROOT_PATH + (i + 1) * step + ".txt";
			sw.start("读取：" + filePath);
			try {
				TextFileReader reader = new TextFileReader(filePath, dataConfigProcess.charsetName());
				while(reader.ready()) {
					dataConfigProcess.parse(reader.readLine());
				}
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (DataParseException e) {
				e.printStackTrace();
			}
			sw.stop();
			System.out.println("读取：" + filePath);
		}
		System.out.println(sw.prettyPrint());
	}
	
	
}
